package com.cxl.demo.mapper;

import com.cxl.demo.vo.Assigment;
import com.cxl.demo.vo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface AssigmentMapper {

    @Select("select * from assigment where assigmentId=#{assigmentId}")

    @Results(value={
            @Result(property = "assigmentId",column = "assigmentId"),
            @Result(property = "introduce",column = "introduce"),
            @Result(property = "startTime",column = "startTime"),
            @Result(property = "endTime",column = "endTime"),
            @Result(property = "placeName",column = "placeName"),
            @Result(property = "placeX",column = "placeX"),
            @Result(property = "placeY",column = "placeY"),
            @Result(property = "needPeopleNumber",column = "needPeopleNumber"),
            @Result(property = "score",column = "score"),
            @Result(property = "userId",column = "userId"),
            @Result(property = "assigmentCondition",column = "assigmentCondition"),
            @Result(property = "getUserNum",column = "assigmentId",
            one =@One(select = "com.cxl.demo.mapper.RecordMapper.recordNumberByAssigmentId")),

            @Result(property = "user",column = "userId",
                    one = @One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId"))
    })
    Assigment findAssigmentById(Integer assigmentId);



    @Select("select * from assigment where userId=#{userId} limit #{page},#{size}")
    List<Assigment> findAssigmentByUserId(Map<String,Object> map);


    List<Assigment> findAssigmentList(Map<String,Object> map);


    Integer getAssigmentNum(Map<String,Object> map);


    @Select("select  count(1) from assigment where userId=#{userId} ")

    Integer getUserAssigmentNum(Integer userId);





    @Insert("insert into assigment (userId,introduce,startTime,endTime,placeName,placeX,placeY,needPeopleNumber,score,assigmentCondition) " +
            "values(#{userId},#{introduce},#{startTime},#{endTime},#{placeName},#{placeX},#{placeY},#{needPeopleNumber},#{score},#{assigmentCondition})")
    @Options(useGeneratedKeys = true, keyProperty = "assigmentId", keyColumn = "assigmentId")
    Integer addAssigment(Assigment assigment);

    @Update("update assigment set assigmentCondition=#{assigmentCondition}, introduce=#{introduce},endTime=#{endTime} ,placeName=#{placeName}, placeX=#{placeX}, placeY=#{placeY}, needPeopleNumber=#{needPeopleNumber}, score=#{score} where assigmentId=#{assigmentId}")
    Integer updateAssigment(Assigment assigment);

    @Delete("delete from assigment where assigmentId=#{assigmentId}")
    Integer deleteAssigment(Integer assigmentId);



    @Select("select * from assigment where assigmentCondition !=1 limit #{page} ,#{size}")



    @Results(value={
            @Result(property = "assigmentId",column = "assigmentId"),
            @Result(property = "introduce",column = "introduce"),
            @Result(property = "startTime",column = "startTime"),
            @Result(property = "endTime",column = "endTime"),
            @Result(property = "placeName",column = "placeName"),
            @Result(property = "placeX",column = "placeX"),
            @Result(property = "placeY",column = "placeY"),
            @Result(property = "needPeopleNumber",column = "needPeopleNumber"),
            @Result(property = "score",column = "score"),
            @Result(property = "userId",column = "userId"),
            @Result(property = "assigmentCondition",column = "assigmentCondition"),
            @Result(property = "user",column = "userId",
                    one = @One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId"))
    })
    List<Assigment> getEmergencyNo1(Map<String,Object> map);
    @Select("select count(1) from assigment where assigmentCondition !=1")
    Integer getEmergencyNo1Num();




    @Update("update assigment set assigmentCondition=#{assigmentCondition}  where assigmentId=#{assigmentId}")
    Integer EditAssigment(Assigment assigment);



    @Select("select * from assigment where assigmentCondition=2 ")


    List<Assigment> getEmergency();


    @Select("select * from assigment  ")
    List<Assigment> findAll();

}
